package com.novunda.platform.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 使用正则表达式验证输入格式
 */
public final class RegexValidateUtil {

    private static final Logger LOG = LoggerFactory.getLogger(RegexValidateUtil.class);


    private RegexValidateUtil() {
    }

    /**
     * 验证邮箱
     *
     * @param email 邮箱验证
     * @return true正确
     */
    public static boolean checkEmail(String email) {
        boolean flag;
        try {
            String check = "^[a-zA-Z0-9]+([._\\-]*[a-zA-Z0-9])*@([a-zA-Z0-9]+[-a-z-A-Z0-9]*[a-zA-Z0-9]+.)(com|cn|net)";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            flag = false;
        }
        return flag;
    }

    /**
     * 验证手机号码
     *
     * @param mobiles 手机号码
     * @return true正确
     */
    public static boolean checkMobileNumber(String mobileNumber) {
        boolean flag;
        try {
            Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
            Matcher matcher = regex.matcher(mobileNumber);
            flag = matcher.matches();
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            flag = false;
        }
        return flag;
    }

}